package com.anzia.map;

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

/**
 *@ClassName TreeMap_Demo
 *@Description
 *@Author Anzia.
 *@Date 2021/4/10 17:39
 *@Version 1.0
 **/
public class TreeMap_Demo {
    public static void main(String[] args) {
        TreeMap<Student, String> treeMap = new TreeMap<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int n = o1.getAge() - o2.getAge();
                return n;
            }
        });

        Student student1 = new Student("孙悟空", 101);
        Student student2 = new Student("沙和尚", 102);
        Student student3 = new Student("猪八戒", 103);

        treeMap.put(student1,"北京");
        treeMap.put(student2,"上海");
        treeMap.put(student3,"杭州");

        System.out.println(treeMap.size());
        System.out.println(treeMap);

        System.out.println("---------");
        for (Student student : treeMap.keySet()) {
            System.out.println(student + " : " + treeMap.get(student));
        }
        System.out.println("---------");
        for (Map.Entry<Student, String> entry : treeMap.entrySet()) {
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }
    }
}
